<template>
  <div class="bg">
    <!-- 登录表单的盒子 -->
    <div class="form-box">
      <h3>超级外卖管理系统</h3>

      <el-form
        :model="loginForm"
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
        size="mini"
      >
        <el-form-item prop="account">
          <el-input
            type="text"
            v-model="loginForm.account"
            autocomplete="off"
            prefix-icon="iconfont icon-yonghu"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            :type="flag ? 'password' : 'text'"
            v-model="loginForm.password"
            autocomplete="off"
            prefix-icon="iconfont icon-mima"
            @keyup.native.enter="submitForm('ruleForm')"
          >
            <!-- 以具名插槽的方式添加了小眼睛 -->
            <template #suffix>
              <i
                class="iconfont"
                :class="flag ? 'icon-eyes' : 'icon-eyes-'"
                @click="flag = !flag"
              ></i>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :loading="loflag"
            @click="submitForm('ruleForm')"
            >登 录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { validateaccount, validatePass } from '../utils/rule'
import { login } from '../api/user.js'
import local from '../utils/local'
import { calcRoutes } from '../router/index.js'
export default {
  data() {
    return {
      // 数据对象
      loginForm: {
        account: '',
        password: ''
      },
      // 规则对象
      rules: {
        // validator 校验器   trigger  触发
        account: [{ validator: validateaccount, trigger: 'blur' }],
        password: [{ validator: validatePass, trigger: 'blur' }]
      },
      flag: true,
      loflag: false
    }
  },
  methods: {
    submitForm(formName) {
      this.loflag = true
      this.$refs[formName].validate(async valid => {
        if (valid) {
          const { account, password } = this.loginForm
          const { code, msg, token, role } = await login({ account, password })
          if (code === 0) {
            local.set('token', token)
            local.set('role', role)
            this.$message.success(msg)
            calcRoutes()
            setTimeout(() => {
              this.$router.push('/')
            }, 2000)
          } else {
            this.$message.error(msg)
            setTimeout(() => {
              this.loflag = false
            }, 2000)
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.bg {
  height: 100%;
  background-image: url('../assets/imgs/拍信素材-186096880.jpg');
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;

  h3 {
    letter-spacing: 4px;
    text-align: center;
    color: #fff;
    font-weight: normal;
    font-size: 16px;
  }
}

.el-form {
  padding-top: 10px;

  .el-button {
    width: 100%;
  }
}

.form-box {
  width: 350px;
}
</style>
